package com.bktech.stupidnews.controlpanel;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bktech.stupidnews.MainActivity;
import com.bktech.stupidnews.R;

public class EditPanel extends Fragment {

	MainActivity activity;
	ExpandableListView expanderListView;
	MyEditListAdapter adapter;
	ArrayList<Category> data;

	OnGroupClickListener onGroupClickListener;
	OnChildClickListener onChildClickListener;
	OnGroupCollapseListener onGroupCollapseListener;
	OnGroupExpandListener onGroupExpandListener;
	private boolean initExpandList = false;
	Button btnRemove;
	Button btnCancel;
	OnClickListener btnOnClickListener;

	public EditPanel(MainActivity activity, ArrayList<Category> data) {
		this.activity = activity;
		this.data = data;
		adapter = new MyEditListAdapter(activity, data);
		btnOnClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (v.getId() == R.id.btn_cancel) {
					EditPanel.this.activity.addSetingPanel(new HomePanel(
							EditPanel.this.activity, EditPanel.this.data));
					EditPanel.this.activity.closeSettingPanel();
				} else {
					adapter.removeRSS(btnRemove);
				}
			}
		};

		onChildClickListener = new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				adapter.checkTo(groupPosition, childPosition, btnRemove);
				return false;
			}
		};

		onGroupClickListener = new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		};

		onGroupCollapseListener = new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				adapter.getCategory(groupPosition).expand = false;

			}
		};

		onGroupExpandListener = new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				adapter.getCategory(groupPosition).expand = true;

			}
		};
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		RelativeLayout layout = (RelativeLayout) inflater.inflate(
				R.layout.setting_panel_edit, container, false);
		expanderListView = (ExpandableListView) layout
				.findViewById(R.id.drawer_list_edit);
		expanderListView.setAdapter(adapter);
		expanderListView.setOnGroupClickListener(onGroupClickListener);
		expanderListView.setOnChildClickListener(onChildClickListener);
		expanderListView.setOnGroupCollapseListener(onGroupCollapseListener);
		expanderListView.setOnGroupExpandListener(onGroupExpandListener);

		btnCancel = (Button) layout.findViewById(R.id.btn_cancel);
		btnCancel.setOnClickListener(btnOnClickListener);
		btnRemove = (Button) layout.findViewById(R.id.btn_remove);
		btnRemove.setOnClickListener(btnOnClickListener);
		if (!initExpandList) {
			initExpandList = true;
			Animation animation = AnimationUtils.loadAnimation(activity,
					R.animator.button_animation);
			LinearLayout layout_button = (LinearLayout) layout
					.findViewById(R.id.table_button);
			layout_button.startAnimation(animation);
			for (int i = 0; i < adapter.listCategory.size(); i++) {
				if (adapter.getCategory(i).expand) {
					expanderListView.expandGroup(i);
				}
			}
		}

		return layout;
	}
}
